﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net;
using VIID;

namespace VIID.Client
{
    /// <summary>
    /// 私有 ROI
    /// </summary>
    public class Rectangle
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    }
    public partial class Client
    {

        /// <summary>
        /// 上报人脸
        /// </summary>
        /// <param name="_ShotTime">抓拍时间</param>
        /// <param name="_SubTypeID">子类型编码 01=视频 02=图像 03=文件 99=其他</param>
        /// <param name="_SubType">01=人员 02=机动车 03=非机动车 04=物品 05=场景 06=人脸 07=视频图像标签 99=其他</param>
        /// <param name="_ROI">特征区域</param>
        /// <param name="_SubImageList">子图集</param>
        /// <param name="_InfoKind">采集方式 1=自动 2=人工</param>
        /// <param name="_GenderCode">性别 0=未知 1=男 2=女 0=未说明</param>
        /// <param name="_AgeUpLimit">年龄上限</param>
        /// <param name="_AgeLowerLimit">年龄下限</param>
        /// <param name="_CapStyle">帽子样式</param>
        /// <param name="_GlassStyle">眼镜样式</param>
        /// <param name="_IsDriver">是否驾驶员 1=是；2=不是</param>
        public bool Face(DateTime _ShotTime, string _SubTypeID, string _SubType, Rectangle _ROI, List<SubImageInfo> _SubImageList, int _InfoKind = 1, string _GenderCode = "0", int _AgeUpLimit = 0, int _AgeLowerLimit = 0, string _CapStyle = "99", string _GlassStyle = "99", int _IsDriver = 2)
        {


            //var _IndexID =  Client.GetIndexId();
            //var _SubTypeID = "02";//
            //var _SubType = "06";//
            //取值范围： 0~41;取值规则：设备编码（20位）+子类型编码（2位）+时间编码（14位）+序号（5位）
            var _SourceID = string.Format("{0:D20}{1:D2}{2:D14}{3:D5}", _options.videoID, _SubTypeID, _ShotTime.ToString("yyyyMMddHHmmss"), _options.OperationNum());
            //取值范围：1~48;设备编码（20位）+子类型编码（2位）+时间编码（14位）+序号（5位）+子类型编码（2位）+序号（5位）；
            var _FaceID = string.Format("{0:D20}{1:D2}{2:D14}{3:D5}{4:D2}{5:D5}", _options.videoID, _SubTypeID, _ShotTime.ToString("yyyyMMddHHmmss"), _options.OperationNum(), _SubType, _options.OperationNum());// "36049900001190000901 02 20210329122124 52872 0652873";

            //创建FACE
            var _FaceObject = new List<Face>();
            _FaceObject.Add(new Face()
            {
                FaceID = _FaceID,
                InfoKind = _InfoKind,
                SourceID = _SourceID,
                DeviceID = _options.videoID,
                //ShotTime = _ShotTime.ToString("yyyyMMddHHmmss"),
                LeftTopX = _ROI.X,
                LeftTopY = _ROI.Y,
                RightBtmX = _ROI.X + _ROI.Width,
                RightBtmY = _ROI.Y + _ROI.Height,
                LocationMarkTime = _ShotTime,
                FaceAppearTime = _ShotTime,
                FaceDisAppearTime = _ShotTime,
                GenderCode = _GenderCode,
                AgeUpLimit = _AgeUpLimit,
                AgeLowerLimit = _AgeLowerLimit,
                CapStyle = _CapStyle,
                GlassStyle = _GlassStyle,
                IsDriver = _IsDriver,
                SubImageList = new SubImageInfoList() { SubImageInfoObject = _SubImageList.ToArray() }
            });

            var _face = new FaceListRoot() //初始化FACE对象
            {
                FaceListObject = new FaceList()
                {
                    FaceObject = _FaceObject.ToArray()
                }
            };


            string postData = _face.ToJSON();// JsonSerializer.Serialize(_face, GAT1400.jsonSetting);
            HttpContent _httpContent = Utils.content(postData);
            HttpResponseMessage response = _httpClient.PostAsync("/VIID/Faces", _httpContent).Result;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var responseJSON = Utils.Stream.read(response.Content.ReadAsStream());
                var o = responseJSON.ToObject<ResponseStatusRoot>();
                if (o != null)
                {
                    if (o.ResponseStatusObject.StatusCode == (int)responseStatus.STATUS_OK)
                    {
                        return true;
                    }
                    Console.WriteLine("{0}:{1}", o.ResponseStatusObject.StatusCode, o.ResponseStatusObject.StatusString);
                }

                return false;
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.Unauthorized)
                    Console.WriteLine("401");
                return false;
            }

            //return (response.StatusCode == HttpStatusCode.OK) ? Utils.Stream.read(response.Content.ReadAsStream()) : response.StatusCode.ToString();

        }

        /// <summary>
        /// 创建过车信息
        /// </summary>
        /// <param name="_ShotTime">抓拍时间</param>
        /// <param name="_SubTypeID">子类型编码 01=视频 02=图像 03=文件 99=其他</param>
        /// <param name="_SubType">01=人员 02=机动车 03=非机动车 04=物品 05=场景 06=人脸 07=视频图像标签 99=其他</param>
        /// <param name="_ROI">特征区域</param>
        /// <param name="_SubImageList">子图集</param>
        /// <param name="_InfoKind">采集方式 1=自动 2=人工</param>
        /// <param name="_LaneNo">车道号</param>
        /// <param name="_PlateNo">车牌号</param>
        /// <param name="_PlateClass">车牌类型</param>
        /// <param name="_PlateColor">车牌颜色</param>
        /// <param name="_Speed">行驶速度</param>
        /// <param name="_VehicleClass">车辆类型</param>
        /// <param name="_VehicleColor">车辆颜色</param>
        /// <param name="_VehicleColorDepth">车辆颜色深浅</param>
        /// <param name="_Sunvisor">遮阳板状态</param>
        /// <param name="_SafetyBelt">安全带状态</param>
        /// <param name="_Calling">打电话状态</param>
        /// <returns></returns>
        public string Vehicle(DateTime _ShotTime, string _SubTypeID, string _SubType, Rectangle _ROI, List<SubImageInfo> _SubImageList, int _InfoKind = 1, int _LaneNo = 1, string _PlateNo = "", string _PlateClass = "0", string _PlateColor = "0", int _Speed = 0, string _VehicleClass = "", string _VehicleColor = "", string _VehicleColorDepth = "", string _Direction = "0", int _Sunvisor = 0, int _SafetyBelt = 0, int _Calling = 0)
        {
            //var _IndexID = _options.OperationNum();
            var _SourceID = string.Format("{0}{1}{2}{3}", _options.videoID, _SubTypeID, _ShotTime.ToString("yyyyMMddHHmmss"), _options.OperationNum());
            var _VehicleID = string.Format("{0}{1}{2}{3}{4}{5}", _options.videoID, _SubTypeID, _ShotTime.ToString("yyyyMMddHHmmss"), _options.OperationNum(), _SubType, _options.OperationNum());// "360499000012180000070220210416134648573660257407";
            var _VehicleObject = new List<MotorVehicle>();


            _VehicleObject.Add(new MotorVehicle()
            {
                MotorVehicleID = _VehicleID,
                InfoKind = _InfoKind,
                SourceID = _SourceID,
                DeviceID = _options.videoID,
                StorageUrl1 = "StoreageUrl1",
                StorageUrl2 = "",
                LeftTopX = _ROI.X,
                LeftTopY = _ROI.Y,
                RightBtmX = _ROI.X + _ROI.Width,
                RightBtmY = _ROI.Y + _ROI.Height,
                LaneNo = _LaneNo,
                HasPlate = string.IsNullOrEmpty(_PlateNo) ? "0" : "1",
                PlateClass = _PlateClass,
                PlateColor = _PlateColor,
                PlateNo = _PlateNo,
                Speed = _Speed,
                VehicleClass = _VehicleClass,
                VehicleColor = _VehicleColor,
                VehicleColorDepth = _VehicleColorDepth,
                PassTime = _ShotTime,
                AppearTime = _ShotTime,
                DisappearTime = _ShotTime,
                MarkTime = _ShotTime,
                Direction = _Direction,
                Sunvisor = _Sunvisor,
                SafetyBelt = _SafetyBelt,
                Calling = _Calling,
                SubImageList = new SubImageInfoList() { SubImageInfoObject = _SubImageList.ToArray() }
            });

            var _vehicle = new MotorVehicleListRoot
            {
                MotorVehicleListObject = new MotorVehicleList()
                {
                    MotorVehicleObject = _VehicleObject.ToArray()

                }
            };

            string postData = _vehicle.ToJSON();// JsonSerializer.Serialize(_vehicle, GAT1400.jsonSetting);
                                                //Console.WriteLine(postData);
            HttpContent _httpContent = Utils.content(postData);


            HttpResponseMessage response = _httpClient.PostAsync("/VIID/MotorVehicles", _httpContent).Result;

            return (response.StatusCode == HttpStatusCode.OK) ? Utils.Stream.read(response.Content.ReadAsStream()) : string.Empty;



        }
        public string Person(DateTime _ShotTime, List<SubImageInfo> _SubImageList)
        {
            var _PersonObject = new List<Person>();

            _PersonObject.Add(new Person()
            {
                AccompanyNumber = 0,
                AgeLowerLimit =0,
                SubImageList = new SubImageInfoList() { SubImageInfoObject = _SubImageList.ToArray() }
            });

            var _person = new PersonListRoot()
            {
                PersonListObject = new PersonList()
                {
                    PersonObject = _PersonObject.ToArray()
                }
            };
            string postData = _person.ToJSON();// JsonSerializer.Serialize(_person, GAT1400.jsonSetting);
                                               //Console.WriteLine(postData);
            HttpContent _httpContent = Utils.content(postData);
            HttpResponseMessage response = _httpClient.PostAsync("/VIID/Persons", _httpContent).Result;
            return (response.StatusCode == HttpStatusCode.OK) ? Utils.Stream.read(response.Content.ReadAsStream()) : string.Empty;
        }


        public bool Person(List<Person> Person)
        {
            var _person = new PersonListRoot()
            {
                PersonListObject = new PersonList()
                {
                    PersonObject = Person.ToArray()
                }
            };

            string postData = _person.ToJSON();
            HttpContent _httpContent = Utils.content(postData);
            HttpResponseMessage response = _httpClient.PostAsync("/VIID/Persons", _httpContent).Result;

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var responseJSON = Utils.Stream.read(response.Content.ReadAsStream());
                var o = responseJSON.ToObject<ResponseStatusRoot>();
                if (o != null)
                {
                    if (o.ResponseStatusObject.StatusCode == (int)responseStatus.STATUS_OK)
                    {
                        return true;
                    }
                    Console.WriteLine("{0}:{1}", o.ResponseStatusObject.StatusCode, o.ResponseStatusObject.StatusString);
                }

                return false;
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.Unauthorized)
                    Console.WriteLine("401");
                return false;
            }
        }
    }
}
